package com.zenith.yasoo.security;

import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import com.zui.base.BaseEntity;
/**
 * 资源表
 * @author SHIJUN
 * @version 1.02008-8-14
 */
@Entity
@Table(name = "security_resource")
@NamedQueries({
	@NamedQuery(name="Resource.level1",query="from Resource where level is null"),
	@NamedQuery(name="Resource.level2",query="from Resource where level = 1"),
	@NamedQuery(name="Resource.level3",query="from Resource where level = 2")
})
public class Resource{
	private static final long serialVersionUID = 1L;

	/**
	 * 资源编码,对应SpringSecurity.xml里的"ROLE_XXXX"
	 */
	private String resourceCode;
	/**
	 * 资源的名称
	 */
	private String resourceName;
	/**
	 * 权限层级
	 */
	private Integer level;

	private Resource parentResource;
	private Set<Resource> childCategories = new LinkedHashSet<Resource>();
	/**
	 * 拥有此资源的所有角色
	 */
	private Set<Role> roles = new LinkedHashSet<Role>();
	/**
	 * 排序编码
	 */
	private String orderIndex;
	/*------------------------------
	 * getters and annotations
	 --------------------------------*/	
	@Id
	@Column(length=50,unique=true)
	public String getResourceCode() {
		return resourceCode;
	}
	
	@Column(length=50)
	public String getResourceName() {
		return resourceName;
	}
	
	public Integer getLevel() {
		return level;
	}
	@ManyToOne() @JoinColumn(name="parentResourceId")
	public Resource getParentResource() {
		return parentResource;
	}
	@OneToMany(mappedBy="parentResource")
	@OrderBy(value="orderIndex asc")
	public Set<Resource> getChildCategories() {
		return childCategories;
	}
	@ManyToMany(mappedBy="resources",fetch = FetchType.LAZY)
	@OrderBy(value="roleName asc")
	public Set<Role> getRoles() {
		return roles;
	}
	@Column(length=20)
	public String getOrderIndex() {
		return orderIndex;
	}
	

	/*------------------------------
	 * setters
	 --------------------------------*/
	public void setResourceCode(String resourceCode) {
		this.resourceCode = resourceCode;
	}
	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}
	public void setLevel(Integer level) {
		this.level = level;
	}
	public void setParentResource(Resource parentResource) {
		this.parentResource = parentResource;
	}
	public void setChildCategories(Set<Resource> childCategories) {
		this.childCategories = childCategories;
	}
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	public void setOrderIndex(String orderIndex) {
		this.orderIndex = orderIndex;
	}	
	
}
